package com.poker.controller;

import com.poker.consts.Code;
import com.poker.entity.User;
import com.poker.service.UserService;
import com.poker.util.Misc;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @Description: 用户信息;
**/
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @RequestMapping("/list")
    public String list(Model model, User user,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "50") Integer size) {
        Page<User> data = userService.findUserNoCriteria(page, size);

        model.addAttribute("data", data);
        model.addAttribute("user", user);
        return managerPage("user/list");
    }

    @RequestMapping("/findQuery")
    public String findQuery(Model model , User user,
                            @RequestParam(defaultValue = "0") Integer page,
                            @RequestParam(defaultValue = "50") Integer size) {
        Page<User> data = userService.findUserCriteria(page, size, user);

        model.addAttribute("data", data);
        model.addAttribute("user", user);
        return managerPage("user/list");
    }

    @ResponseBody
    @PostMapping("/unlock")
    public String unlock(@RequestParam String id) {
        if (Misc.isEmpty(id)) return toJson(false, "id信息为空", null);

        User user = userService.findById(id);
        if (Misc.isEmpty(user)) return toJson(false, "用户信息不存在", null);

        if (!Code.UserState.LOCKED.equals(user.getUserState()))
            return toJson(false, "用户状态不是锁定状态", null);
        userService.updateUserStateToNormal(user, loginManager());

        return toJson(true, "success", null);
    }

}
